﻿$(() => {
    const $container = $('body > .container');
    //查看报告
    $container.on('viewReport', (_, data) => {
        getOids(data).then(oids => {
            if (oids.length === 0) {
                DevExpress.ui.notify('请选择需要查看报告的检查', 'error');
            } else {
                ris.ajax(`/api/report/get-report-pdf-files`, { type: 'POST', data: { examIds: oids }, errorMessage: '打印服务启动失败' }).then(result => {
                    if (result.success) {
                        if (result.error) {
                            DevExpress.ui.notify(result.error, 'error');
                        }
                        result.data.forEach(e => {
                            ris.viewReport(e);
                        })
                    } else {
                        DevExpress.ui.notify(result.error, 'error');
                    }
                })
            }
        });
    });
    //查看图像
    $container.on('viewImage', (_, data) => {
        getOids(data).then(ids => {
            if (ids.length === 0) {
                DevExpress.ui.notify('请选择需要查看图像的检查', 'error');
            } else {
                ris.ajax('/api/exam/get-dicom-client-url').then(result => {
                    if (result.success && result.data) {
                        let url = result.data;
                        if (result.data.includes('?')) {
                            url += `&examIds=${ids.join(',')}`;
                        } else {
                            url += `?examIds=${ids.join(',')}`;
                        }
                        ris.viewImage(url);
                    } else {
                        DevExpress.ui.notify(result.error, 'error');
                    }
                });
            }
        })
    });

    // 获取Oids
    const getOids = (data) => {
        return new Promise(resolve => {
            // 获取多选数据
            const { examGrid } = data;
            let oids = (examGrid && examGrid.getSelectedRowKeys());
            resolve(oids ? oids : []);
        })
    }
})